package com.gk.panda.apis.controller;

import com.alibaba.nacos.common.utils.HttpMethod;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gk.panda.apis.config.IccConfig;
import com.gk.panda.apis.entity.deviceManager.dto.CloudDirectionDto;
import com.gk.panda.apis.entity.deviceManager.dto.DeviceListDto;
import com.gk.panda.apis.entity.deviceManager.dto.SaveOrChangeDeviceDataDto;
import com.gk.panda.apis.entity.deviceManager.vo.DeviceManagerDetailsVo;
import com.gk.panda.apis.entity.deviceManager.vo.DeviceManagerListVo;
import com.gk.panda.apis.entity.icc.dto.CameraDto;
import com.gk.panda.apis.entity.icc.vo.IccDeviceVo;
import com.gk.panda.apis.entity.icc.vo.VideoListVo;
import com.gk.panda.apis.log.Log;
import com.gk.panda.apis.service.DeviceManagerService;
import com.gk.panda.commons.result.Result;
import com.gk.panda.pojo.DeviceManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @ClassName: DeviceManagerController
 * @Description: PC设备管理
 * @author: Sir.yang
 * @date: 2022/11/8 18:45
 * @version: 1.0
 **/
@RestController
@Api(tags = "PC设备管理")
@RequestMapping("/edmp/device")
public class DeviceManagerController {

    @Autowired
    private DeviceManagerService deviceManagerService;

    @Autowired
    private IccConfig iccConfig;

    @ApiOperation("获取设备列表")
    @PostMapping("/list")
    public Result<Page<DeviceManagerListVo>> getDevicePage(@RequestBody DeviceListDto dto) {
        return Result.ok(deviceManagerService.getDevicePage(dto));
    }

    @ApiOperation("根据当前登录用户获取所属机构及下属机构红外相机列表")
    @GetMapping("/infraredCameraListByCurrentUser")
    public Result<List<DeviceManager>> infraredCameraListByCurrentUser() {
        return Result.ok(deviceManagerService.infraredCameraListByCurrentUser());
    }

    @ApiOperation("根据设备ID获取设备详情")
    @GetMapping("/getDetails/{devicdId}")
    public Result<DeviceManagerDetailsVo> getDeviceDetailsById(@PathVariable Integer devicdId) {
        return Result.ok(deviceManagerService.getDeviceDetailsById(devicdId));
    }

    @ApiOperation("修改设备信息")
    @Log
    @PostMapping("/changeDeviceData")
    public Result changeDeviceData(@RequestBody SaveOrChangeDeviceDataDto dto) throws UnsupportedEncodingException {
        return Result.ok(deviceManagerService.changeDeviceData(dto));
    }

    @ApiOperation("新增设备信息")
    @Log
    @PostMapping("/saveDeviceData")
    public Result saveDeviceData(@RequestBody SaveOrChangeDeviceDataDto dto) throws UnsupportedEncodingException {
        return Result.ok(deviceManagerService.saveDeviceData(dto));
    }

    @ApiOperation("删除设备信息")
    @Log
    @PostMapping("/removeDevice/{devicdId}")
    public Result removeDeviceById(@PathVariable Integer devicdId) throws UnsupportedEncodingException {
        return Result.ok(deviceManagerService.removeDeviceById(devicdId));
    }

    @ApiOperation("云台方向控制")
    @Log
    @PostMapping("/operateDirect")
    public Result<String> operateDirect(@RequestBody CloudDirectionDto directionDto) {
        int i = deviceManagerService.operateDirect(directionDto);
        if (i == -1) return Result.write(201,"设备不支持或出错");
        if (i == -2) return Result.write(201,"控制出错或设备不支持");
        return Result.write(200,"控制成功");
    }

    @ApiOperation("ICC设备列表查询")
    @PostMapping("/iccDeviceListQuery")
    public Result<List<IccDeviceVo>> iccDeviceListQuery() {
        return Result.ok(deviceManagerService.iccDeviceListQuery());
    }

    @ApiOperation("ICC获取野保相机图片视频列表")
    @PostMapping("/cameraList")
    public Result<List<VideoListVo>> cameraList(@RequestBody CameraDto cameraDto){
        return Result.ok(deviceManagerService.cameraList(cameraDto));
    }

    @ApiOperation("保存或更新ICC数据")
    @Log
    @PostMapping("/saveDeviceFromIcc")
    public Result<String> saveDeviceFromIcc() throws UnsupportedEncodingException {
        return Result.ok(deviceManagerService.saveDeviceFromIcc());
    }

    @ApiOperation("获取摄像机实时流")
    @GetMapping("/getVideoLiveStream")
    public Result<String> getVideoLiveStream(String deviceSn) {
        return Result.ok(deviceManagerService.getVideoLiveStream(deviceSn));
    }


    @ApiOperation(value = "下载红外相机导入模板", httpMethod = HttpMethod.GET)
    @GetMapping("/infrared/template")
    public void getInfraredTemplate(HttpServletResponse response) {
        deviceManagerService.getInfraredTemplate(response);
    }

    @ApiOperation(value = "下载摄像机导入模板", httpMethod = HttpMethod.GET)
    @GetMapping("/video/template")
    public void getVideoTemplate(HttpServletResponse response) {
        deviceManagerService.getVideoTemplate(response);
    }

    @ApiOperation(value = "下载生态设备导入模板", httpMethod = HttpMethod.GET)
    @GetMapping("/ecological/template")
    public void getEcologicalTemplate(HttpServletResponse response) {
        deviceManagerService.getEcologicalTemplate(response);
    }


    @ApiOperation(value = "批量导入红外相机信息", httpMethod = HttpMethod.POST)
    @Log
    @PostMapping("/infrared/import")
    public Result<String> importInfrared(@RequestPart("file") MultipartFile file) throws UnsupportedEncodingException {
        String result =  deviceManagerService.importInfrared(file);
        if (result.equals("success")) {
            return Result.write(200,"导入成功");
        } else if (result.equals("failed")) {
            return Result.write(201,"导入失败");
        } else {
            return Result.write(201,result);
        }
    }

    @ApiOperation(value = "批量导入摄像机信息", httpMethod = HttpMethod.POST)
    @Log
    @PostMapping("/video/import")
    public Result<String> importVideo(@RequestPart("file") MultipartFile file) throws UnsupportedEncodingException {
        String result =  deviceManagerService.importVideo(file);
        if (result.equals("success")) {
            return Result.write(200,"导入成功");
        } else if (result.equals("failed")) {
            return Result.write(201,"导入失败");
        } else {
            return Result.write(201,result);
        }
    }

    @ApiOperation(value = "批量导入生态设备信息", httpMethod = HttpMethod.POST)
    @Log
    @PostMapping("/ecological/import")
    public Result<String> importEcological(@RequestPart("file") MultipartFile file) throws UnsupportedEncodingException {
        String result =  deviceManagerService.importEcological(file);
        if (result.equals("success")) {
            return Result.write(200,"导入成功");
        } else if (result.equals("failed")) {
            return Result.write(201,"导入失败");
        } else {
            return Result.write(201,result);
        }
    }

    @ApiOperation(value = "批量导出红外相机信息", httpMethod = HttpMethod.GET)
    @Log
    @GetMapping("/infrared/export")
    public void exportInfrared(HttpServletResponse response) {
        deviceManagerService.exportInfrared(response);
    }

    @ApiOperation(value = "批量导出摄像机信息", httpMethod = HttpMethod.GET)
    @Log
    @GetMapping("/video/export")
    public void exportVideo(HttpServletResponse response) {
        deviceManagerService.exportVideo(response);
    }

    @ApiOperation(value = "批量导出生态设备信息", httpMethod = HttpMethod.GET)
    @Log
    @GetMapping("/ecological/export")
    public void exportEcological(HttpServletResponse response) {
        deviceManagerService.exportEcological(response);
    }

}
